const app = getApp();
Page({

  data: {
    work_pics:[],
    security_pics:[],
    other_pics:[],
    click_button:0
  },

  onLoad: function() {
    var that = this;
    that.getProveInfo();
  },

  onShow: function () {
    let that = this;
  },

  getProveInfo: function(e) {
    var that = this;
    wx.request({
      url: app.globalData.domain + 'index/getProveInfo',
      method: "POST",
      data: {
        user_id: wx.getStorageSync('userinfo').id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: (res) => {
        console.log(res.data.info);
        if(res.data.info != ""){
          that.setData({
            work_pics: [res.data.info.work],
            security_pics: [res.data.info.security]
          })
          if(res.data.info.other != ""){
            that.setData({
              other_pics: [res.data.info.other]
            })
          }
        }
      },
    })
  },

  updataWorkImg: function(e) {
    var that = this;
    wx.chooseImage({
      count: 1, 
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        if (res.tempFilePaths) {
          var counts=res.tempFilePaths.length + that.data.work_pics.length;
          if(counts>1){
            wx.showToast({
              title: '最多上传1张图片',
              icon: 'none',
              duration: 2000,
            })
          }else{
            for (var i = 0; i < res.tempFilePaths.length; i++) {
              wx.uploadFile({
                url: app.globalData.domain + 'index/uploadImages',
                filePath: res.tempFilePaths[i],
                name: 'file',
                success: function(r) {
                  that.data.work_pics.push(r.data);
                  that.setData({
                    work_pics: that.data.work_pics
                  });
                }
              })
            }
          }
        }
      }
    })
  },

  delectWorkImg: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.work_pics.splice(index, 1);
    that.setData({
      work_pics: that.data.work_pics
    })
  },

  updataSecurityImg: function(e) {
    var that = this;
    wx.chooseImage({
      count: 1, 
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        if (res.tempFilePaths) {
          var counts=res.tempFilePaths.length + that.data.security_pics.length;
          if(counts>1){
            wx.showToast({
              title: '最多上传1张图片',
              icon: 'none',
              duration: 2000,
            })
          }else{
            for (var i = 0; i < res.tempFilePaths.length; i++) {
              wx.uploadFile({
                url: app.globalData.domain + 'index/uploadImages',
                filePath: res.tempFilePaths[i],
                name: 'file',
                success: function(r) {
                  that.data.security_pics.push(r.data);
                  that.setData({
                    security_pics: that.data.security_pics
                  });
                }
              })
            }
          }
        }
      }
    })
  },

  delectSecurityImg: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.security_pics.splice(index, 1);
    that.setData({
      security_pics: that.data.security_pics
    })
  },

  updataOtherImg: function(e) {
    var that = this;
    wx.chooseImage({
      count: 1, 
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        if (res.tempFilePaths) {
          var counts=res.tempFilePaths.length + that.data.other_pics.length;
          if(counts>1){
            wx.showToast({
              title: '最多上传1张图片',
              icon: 'none',
              duration: 2000,
            })
          }else{
            for (var i = 0; i < res.tempFilePaths.length; i++) {
              wx.uploadFile({
                url: app.globalData.domain + 'index/uploadImages',
                filePath: res.tempFilePaths[i],
                name: 'file',
                success: function(r) {
                  that.data.other_pics.push(r.data);
                  that.setData({
                    other_pics: that.data.other_pics
                  });
                }
              })
            }
          }
        }
      }
    })
  },

  delectOtherImg: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.other_pics.splice(index, 1);
    that.setData({
      other_pics: that.data.other_pics
    })
  },

  big_preview: function (e) {
    var url = e.currentTarget.dataset.url;
    var urls = e.currentTarget.dataset.urls;
    var previewImgArr = urls;
    wx.previewImage({
      current: url,
      urls: previewImgArr
    })
  },

  submit_result:function(){
    var that = this;
    if (that.data.work_pics.length==0) {
      wx.showToast({
        title: '请上传岗位证明',
        icon: 'none',
        duration: 2000,
      })
    } else if (that.data.security_pics.length==0) {
      wx.showToast({
        title: '请上社保证明',
        icon: 'none',
        duration: 2000,
      })
    }  else {
      this.setData({
        click_button: 1
      })
      wx.request({
        url: app.globalData.domain + 'index/updateProve',
        method: "POST",
        data: {
          work_pics: that.data.work_pics,
          security_pics: that.data.security_pics,
          other_pics: that.data.other_pics,
          user_id: wx.getStorageSync('userinfo').id
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function(res) {
          if (res.data.code == 0) {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000,
              success: function() {
                that.setData({
                  click_button:0
                })
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1
                  });
                }, 1000);
              }
            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000,
              success: function() {}
            })
          }
        }
      })
    }
  },

})